package com.daryl.sqlSession;

import com.daryl.config.Function;
import com.daryl.config.MapperBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;

public class MyMapperProxy implements InvocationHandler {
    private MySqlsession mysqlsession;
    private MyConfiguration myConfiguration;


    public MyMapperProxy(MyConfiguration myConfiguration, MySqlsession mysqlsession) {
        this.myConfiguration = myConfiguration;
        this.mysqlsession = mysqlsession;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        MapperBean readMapper = myConfiguration.readMapper("UserMapper.xml");
        if(!method.getDeclaringClass().getName().equals(readMapper.getInterfaceName())){
            return null;
        }
        List<Function> list = readMapper.getList();
        if(null != list || 0 != list.size()){
            for(Function function : list){
                if(method.getName().equals(function.getFuncName())){
                    return mysqlsession.selectOne(function.getSql(),String.valueOf(args[0]));
                }
            }
        }
        return null;
    }
}
